package components.editor
{
	import UIFree.uif_component;
	
	import UIUtils.uif_matrix;
	
	import classes.c_events;
	
	import components.c_component;
	import components.c_control;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class c_dragRectHandle extends c_control {
		private var _startRatio : Number;	
		private var _propResize	: Boolean;
		
		public function c_dragRectHandle(parent:uif_component, a_data: Object, a_size: int, styleClass: Object) {
			super(parent, 0, 0, a_size, a_size, styleClass);
			_data 		= a_data;
			_propResize = false;
		}
		
		public function get dragRect(): c_dragRect {
			return parent.parent as c_dragRect;
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(MouseEvent.MOUSE_DOWN, doMouseDown);
			addEvent(uif_component.STARTDRAG, doStartDrag);
			addEvent(uif_component.ENDDRAG, doEndDrag);
			addEvent(KeyboardEvent.KEY_DOWN, doKeyDown, stage);
			addEvent(KeyboardEvent.KEY_UP, doKeyUp, stage);
		}
		
		protected function doMouseDown(e: MouseEvent): void {
			beginDrag(2);
		}
		
		public function doStartDrag(e: Event): void {
			_startRatio = dragRect.size.x / dragRect.size.y;
			dragRect.startEditing();
		}
		
		public function doEndDrag(e: Event): void {
			dragRect.endEditing();
		}
		
		protected function doKeyDown(e: KeyboardEvent): void {
			if ((e.keyCode == 16) && !_propResize) {
/*				if (dragState) {
					var rect: Rectangle = dragRect.getOuter();
					var s: Point 		= _beforeSize.clone();
					s.normalize(rect.size.length);
					switch (_data) {
						case 1: rect.y += rect.size.y - s.y;
							break;
						case 5: rect.x += rect.size.x - s.x;
							break;
					}
					rect.size = s;
					dragRect.setRect(rect);
				}*/
				_propResize 	= true;
			}
		}
		
		protected function doKeyUp(e: KeyboardEvent): void {
			if ((e.keyCode == 16) && _propResize) {
				_propResize 	= false;
			}
		}
		
		protected function get propResize(): Boolean {
			return _propResize;
		}
		
		protected function checkNewRect(rect: Rectangle): Boolean {
			return (rect.right - rect.left >= c_dragRect.MINSIZE) && (rect.bottom - rect.top >= c_dragRect.MINSIZE);
		}
		
		override protected function updateDragPosition(n_x: Number, n_y: Number): Boolean {
			var c			: Point 		= dragRect.center;
			var rollMatrix	: Matrix		= dragRect.transform.matrix.clone();
			rollMatrix.translate(-dragRect.x, -dragRect.y);
/*			
			var p: Point = new Point(0, 20);
			p = rollMatrix.transformPoint(p);
*/
			
			function proj(a: Point, b: Point): Point {
				var dp: Number = a.x*b.x + a.y*b.y; 
				return new Point(( dp / (b.x*b.x + b.y*b.y) ) * b.x,
									( dp / (b.x*b.x + b.y*b.y) ) * b.y);
			}
			
			var size		: Point;
			var delta		: Point = new Point(n_x - x, n_y - y);
			if (_propResize) {
				size = new Point(_startRatio, 1);
				size.normalize(dragRect.size.length);
				
				switch (_data) {
					case 0: delta = proj(delta, new Point(0, -size.y));
						break;
					case 1: delta = proj(delta, new Point(size.x, -size.y));
						break;
					case 5: delta = proj(delta, new Point(-size.x, size.y));
						break;
					default: delta = proj(delta, size);
						break;
				}
			} else {
				size = dragRect.size.clone();
			}
			
			var p			: Point		= new Point(); 
			var mat			: Matrix 	= new Matrix();
			switch (_data) {
				case 0: size.y -= delta.y;
						p.y += delta.y;
						break;
				case 1: size.y -= delta.y;
						size.x += delta.x;
						p.y += delta.y;
						break;
				case 2: size.x += delta.x;
						break;
				case 3: size.y += delta.y;
						size.x += delta.x;
						break;
				case 4: size.y += delta.y;
						break;
				case 5: size.y += delta.y;
						size.x -= delta.x;
						p.x += delta.x;
						break;
				case 6: size.x -= delta.x;
						p.x += delta.x;
						break;
				case 7: size.y -= delta.y;
						p.y += delta.y;
						size.x -= delta.x;
						p.x += delta.x;
						break;
				case 'ROLL':
					
					var v1: Point 	= new Point(x - c.x, y - c.y); 
					var v2: Point 	= new Point(n_x - c.x, n_y - c.y);
					
					var angle: Number = Math.atan2(v1.x, v1.y) - Math.atan2(v2.x, v2.y);
					dragRect.rollMatrix(angle);
					return false;
					break;
			}
						
//			if (checkNewRect(rect)) {
				dragRect.position	= dragRect.position.add(rollMatrix.transformPoint(p));
				dragRect.size 		= size;
				return true;
//			} else return false;
		}
		
	}
}